
/**
 * Project Name:cats-core-impl
 * File Name:AuthAreaServiceImpl.java
 * Package Name:com.catsic.core.autharea.service.impl
 * Date:2014年3月24日下午3:38:48
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.autharea.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.autharea.dao.AuthAreaDao;
import com.catsic.core.autharea.service.AuthAreaService;
import com.catsic.core.constants.Constants;
import com.catsic.core.convert.Converts;
import com.catsic.core.entity.AuthArea;
import com.catsic.core.entity.Menu;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.form.AuthAreaForm;
import com.catsic.core.menu.service.MenuService;

/**
 * ClassName: AuthAreaServiceImpl
 * Description: 授权区域Service
 * date: 2014年3月24日 下午3:38:48
 * author: yangyd
 */
@Service("authAreaService")
public class AuthAreaServiceImpl extends BaseServiceImpl<AuthArea, String> implements AuthAreaService{

	@Autowired
	private AuthAreaDao authAreaDao;
	
	@Autowired
	private MenuService menuService;
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#add(com.catsic.core.form.AuthAreaForm)
	 **/
	@Override
	public AuthAreaForm add(AuthAreaForm form){
		AuthArea authArea = new AuthArea();
		if(StringUtils.isNotBlank(form.getPid())){
			AuthArea parent = authAreaDao.findByCode(form.getPid());
			authArea.setParent(parent);
		}
		
		Menu resource = menuService.findByCode(form.getResourceCode());
		authArea.setResource(resource);
		authArea.setName(form.getName());
		authArea.setCode(code(form));
		authArea = super.add(authArea);
		return Converts.convert(authArea);
	}
	
	/**
	 * 生成编码规则
	 * @param pid
	 * @return
	 */
	private String code(AuthAreaForm form){
		if(StringUtils.isBlank(form.getPid())){
			List<AuthArea> childs = authAreaDao.findByPidIsNullAndResourceCodeOrderByCodeDesc(form.getResourceCode());
			if(CollectionUtils.isEmpty(childs)){
				return form.getResourceCode() + String.format("%0" + Constants.编码位数 + "d",0);
			}
			String code = childs.get(0).getCode();
			int length = code.length();
			return String.format("%0" + length + "d", Long.valueOf(code)+ 1);
		}
		/**
		 * 查询父节点下的所有子节点
		 **/
		List<AuthArea> childs = authAreaDao.findByCodeIsNotNullAndParentCodeOrderByCodeDesc(form.getPid());
		AuthArea parent = findById(form.getPid());
		if(CollectionUtils.isNotEmpty(childs)){
			String code = childs.get(0).getCode();
			int length = code.length();
			return String.format("%0" + length + "d", Long.valueOf(code)+ 1);
		}else {
			return parent == null ? String.format("%0" + Constants.编码位数 + "d",0) : parent.getCode() + String.format("%0" + Constants.编码位数 + "d",0);
		}
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#update(com.catsic.core.form.AuthAreaForm)
	 */
	public AuthAreaForm update(AuthAreaForm form){
		AuthArea authArea = findById(form.getId());
		authArea.setName(form.getName());
		authArea.setCode(form.getCode());
		authArea.setResource(menuService.findByCode(form.getResourceCode()));
		authArea = super.add(authArea);
		return Converts.convert(authArea);
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findById(java.lang.Long)
	 */
	@Override
	public AuthArea findById(String id){
		AuthArea authArea = getById(id);
		if(null == authArea){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.AUTHAREA);
		}
		return authArea;
	}
		
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#delete(java.lang.String)
	 */
	@Override
	public void delete(String id){
		AuthArea authArea = findById(id);
		authAreaDao.delete(authArea);
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findByPcode(java.lang.String)
	 */
	public List<AuthArea> findByPcode(String code){
		return authAreaDao.findByPcode(code);
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findByCode(java.lang.String)
	 */
	@Override
	public AuthArea findByCode(String code) {
		AuthArea authArea = authAreaDao.findByCode(code);
		if(authArea == null){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.AUTHAREA);
		}
		return authArea;
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#getTreeInfo(java.lang.String)
	 */
	public List<TreeInfo> getTreeInfo(String resourceCode){
		List<AuthArea> list = new ArrayList<AuthArea>();
		/**
		 * StringUtils.isBlank(id)条件为点击主页面权限管理功能时触发
		 */
		list = authAreaDao.findByResourceCode(resourceCode);
		List<TreeInfo> listTreeInfo = new ArrayList<TreeInfo>();
		for(AuthArea authArea : list){
			TreeInfo treeInfo = Converts.convertTree(authArea);
			listTreeInfo.add(treeInfo);
		}
		return listTreeInfo;
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#query(java.lang.String)
	 */
	public List<AuthAreaForm> query(String code){
		List<AuthArea> list = authAreaDao.findByPcode(code);
		List<AuthAreaForm> listForm = new ArrayList<AuthAreaForm>();
		for(AuthArea authArea : list){
			AuthAreaForm form = Converts.convert(authArea);
			listForm.add(form);
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findByPidIsNullAndResourceCodeOrderByCodeDesc(java.lang.String)
	 */
	public List<AuthArea> findByPidIsNullAndResourceCodeOrderByCodeDesc(String resourceCode){
		List<AuthArea> list = authAreaDao.findByPidIsNullAndResourceCodeOrderByCodeDesc(resourceCode);
		return list;
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findFormByPidIsNullAndResourceCodeOrderByCodeDesc(java.lang.String)
	 */
	public List<AuthAreaForm> findFormByPidIsNullAndResourceCodeOrderByCodeDesc(String resourceCode){
		List<AuthArea> list = findByPidIsNullAndResourceCodeOrderByCodeDesc(resourceCode);
		List<AuthAreaForm> listForm = new ArrayList<AuthAreaForm>();
		for(AuthArea authArea : list){
			AuthAreaForm form = Converts.convert(authArea);
			listForm.add(form);
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findByResourceCode(java.lang.String)
	 */
	public List<AuthArea> findByResourceCode(String resourceCode){
		return authAreaDao.findByResourceCode(resourceCode);
	}
	
	/**
	 * @see com.catsic.core.autharea.service.AuthAreaService#findFormByResourceCode(java.lang.String)
	 */
	public List<AuthAreaForm> findFormByResourceCode(String resourceCode){
		List<AuthArea> list = findByResourceCode(resourceCode);
		List<AuthAreaForm> listForm = new ArrayList<AuthAreaForm>();
		for(AuthArea authArea : list){
			AuthAreaForm form = Converts.convert(authArea);
			listForm.add(form);
		}
		return listForm;
	}
}
